package com.lyb.spzx.user.controller;

import com.lyb.common.utils.AuthContextUtil;
import com.lyb.spzx.model.dto.system.system.user.UserInfo;
import com.lyb.spzx.model.vo.h5.UserInfoVo;
import com.lyb.spzx.user.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.lyb.common.utils.IpUtil;
import com.lyb.spzx.model.dto.h5.UserLoginDto;
import com.lyb.spzx.model.dto.h5.UserRegisterDto;
import com.lyb.spzx.model.vo.common.Result;
import com.lyb.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @Operation(summary = "注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request){
        String ip = IpUtil.getIpAddress(request);
        String token = userInfoService.login(userLoginDto, ip);
        return Result.build(token,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(){
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }



}
